package com.pro.admin.web.guanwang;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro.db.service.itf.IGuopuMessageboardService;
import com.pro.admin.annotation.RequiresPermissionsDesc;
import com.pro.admin.web.BaseController;
import com.pro.core.util.ResponseUtil;
import com.pro.db.domain.GuopuMessageboard;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 留言板 前端控制器
 * </p>
 *
 * @author wanglei
 * @since 2019-12-22
 */
@RestController
@RequestMapping("/admin/msgboard")
public class GuopuMessageboardController extends BaseController {
    private final Log logger = LogFactory.getLog(GuopuMessageboardController.class);

    @Autowired
    private IGuopuMessageboardService messageboardService;

    /**
     * @apiDefine msgboard 留言板接口
     *
     */

    /**
     * @api {GET} /admin/msgboard/list 5.1 留言板列表（官网加入我们提交的信息）
     * @apiName msgboard list
     * @apiGroup msgboard
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:msgboard:list")
    @RequiresPermissionsDesc(menu={"留言板管理" , "留言板设置"}, button="留言板列表")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<GuopuMessageboard> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<GuopuMessageboard> pList = messageboardService.page(pageVo,
                new QueryWrapper<GuopuMessageboard>()
                        .eq("deleted",0)
                        .orderByDesc("add_time"));

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }


    /**
     * @api {GET} /admin/msgboard/read 5.2 留言板详情
     * @apiName msgboard read
     * @apiGroup msgboard
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 留言板id
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:msgboard:read")
    @RequiresPermissionsDesc(menu={"留言板管理" , "留言板设置"}, button="留言板详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        GuopuMessageboard banner = messageboardService.getById(id);
        return ResponseUtil.ok(banner);
    }

    /**
     * @api {POST} /admin/msgboard/delete 5.3 删除留言
     * @apiName msgboard delete
     * @apiGroup msgboard
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 留言id
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:msgboard:delete")
    @RequiresPermissionsDesc(menu={"留言板管理" , "留言板设置"}, button="删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody GuopuMessageboard banner) {
        Integer id = banner.getId();
        if (id == null) {
            return ResponseUtil.fail("ID不能为空");
        }
        messageboardService.removeById(id);
        return ResponseUtil.ok();
    }

}

